<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Custom CodeIgniter Debug Helpers
 *
 * @package		Exa
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Exa
 * @link		NA
 */

// ------------------------------------------------------------------------		

/**
 * Debug
 *
 * This will print the line no, file name, function name, class name , class method name and comments
 * This is ued to print the information in a proper format. 
 *
 * @access public
 * @param string
 * @param string
 * @param string
 * @param string
 * @param string
 * @param string	
 * @return void
 *
 * Example : debug(__LINE__,__FILE__,__FUNCTION__,__CLASS__,__METHOD__,$comments); 
 */
if( ! function_exists("debug") ) 
{
	function debug($line='',$file='',$function='',$class='',$method='',$comments='')
	{
		$obj =& get_instance();

		if(CALL_DEBUG == TRUE)
		{
			$str = "";
			
			$str .= ($line) 	? '<br>The current line number of the file :<strong>'.$line.'</strong>':'';
			$str .= ($file) 	? '<br>The full path and filename of the file :<strong>'.$file.'</strong>':'';			
			$str .= ($function) ? '<br>The function name :<strong>'.$function.'</strong>':'';				
			$str .= ($class) 	? '<br>The class name :<strong>'.$class.'</strong>':'';
			$str .= ($method) 	? '<br>The class method name :<strong>'.$method.'</strong>':'';
			$str .= ($comments) ? '<br>Comments :<strong>'.$comments.'</strong>':'';
			$str .= "<br/>===========================================";
			
			if(CALL_BACK_TREE == TRUE)
			{
				//$str .=	'<br>Backtree:'.( debug_print_backtrace() );
			}
							
			$obj->config->config['debug_message'][] = $str;
		}	
	}
}

// ------------------------------------------------------------------------		

/**
 * Default Value
 *
 * This will set the default value
 *
 * @access 	public
 * @param	string
 * @return 	string
 *
 * Example : default_value($value); 
 */
if( ! function_exists("default_value") ) 
{
	function default_value($data_value, $return_value = '', $compare_with = '')
	{
		if(!empty($data_value) && strtoupper($data_value) != 'NULL' && $data_value != $compare_with)
		{
			return $data_value;	
		}		
		else
		{
			if ($return_value != '')
			{
				return $return_value;
			}
			else
			{
				return '--';
			}
		}
	}
}

// ------------------------------------------------------------------------		

/**
 * Default Value Blank
 *
 * This will set the default value
 *
 * @access 	public
 * @param	string
 * @return 	string
 *
 * Example : default_value_blank($value); 
 */
if( ! function_exists("default_value_blank") ) 
{
	function default_value_blank($data_value, $return_value = '', $compare_with = '')
	{
		if(!empty($data_value) && strtoupper($data_value) != 'NULL' && $data_value != $compare_with)
		{
			return $data_value;	
		}		
		else
		{
			return $return_value;
		}
	}
}

// ------------------------------------------------------------------------		

/**
 * Filter Data
 *
 * This will filter the data based on the mode passed
 *
 * @access 	public
 * @param	string
 * @param 	string
 * @return 	string
 *
 * Example : filter_data($value,'htmlentities'); 
 */
if( ! function_exists("filter_data") ) 
{
	function filter_data($data_value='',$mode='')
	{
    $CI =& get_instance();
		$value = '';
		switch($mode)
		{
			default:		
			case 'htmlentities':
				$value = htmlentities(trim($data_value), ENT_QUOTES, "UTF-8");
			break;
			
			case 'htmlspecialchars':
				$value = htmlspecialchars ($data_value);
			break;
    
      case 'display':
				$value = html_entity_decode ($data_value);
			break;
    
      case 'none':
				$value = ($data_value);
			break;
    
      case 'cstr':
      case 'qry':
				$value = htmlentities( strip_tags( trim($data_value) ));
			break;
    
      case 'cint':
				$value = (int) $data_value;
			break;
    
      case 'insert':
      case 'like':
        $value = $CI->db->escape_like_str($data_value);
      break;
    
      case 'search_like':        
        $value = "LIKE CONVERT(_utf8 '%".filter_data($data_value)."%' USING latin1) COLLATE latin1_swedish_ci";
        //$value = " LIKE '%".filter_data($data_value)."%' ";
      break;
      
		}
		
		return $value;
	}
}

// ------------------------------------------------------------------------		

/**
 * Set Date Picker date
 *
 * @access 	public
 * @param	string
 * @param	string
 * @param	string
 * @return 	string
 *
 */
if( ! function_exists("set_datepicker_date") ) 
{
	//function set_datepicker_date($date='',$format='Y-m-d', $return_value = 'N/A')
	function set_datepicker_date($date='',$format='d-m-Y', $return_value = 'N/A')
	{
		if(
				!empty($date) 
			&& 	$date != 'NULL' 
			&& 	$date != '0000-00-00 00:00:00' 
			&& 	$date != '1970-01-01 00:00:00' 
			&& 	$date != '1970-01-01 10:00:00' 
			&& 	$date != '1900-01-01 00:00:00'
      && 	$date != '1970-01-01 01:00:00'            
			&& 	$date != '1900-01-01' 
			&& 	$date != '1970-01-01' 
			&& 	$date != 'N/A' 
			&& 	$date != 'NA'
			&&	$date != '--') 
		{
			return date($format,strtotime($date));
		}	
		else
		{
			return '';
		}	
	}
}


// ------------------------------------------------------------------------		

/**
 * Set Fax number format
 *
 * @access 	public
 * @param	string
 * @param	string
 * @param	string
 * @return 	string
 *
 */
if( ! function_exists("get_fax") ) 
{
	function get_fax($fax_num='')
	{
		if(!empty($fax_num) && $fax_num != 'NULL' && $fax_num != 'N/A' && $fax_num != 'NA') 
		{
			$new_num = str_replace(' ', '', $fax_num);
			$pos = strpos($new_num, '03');
			
			if ($pos == 0) {
				$new_num = substr_replace($new_num, '61', 0,1);
			}
			
			return '+'.$new_num.'@fax.tc';
		}	
		else
		{
			return false;
		}	
	}
}

// ------------------------------------------------------------------------		

/**
 * Check user admin or client
 *
 * @access 	public
 * @param	string
 * @param	string
 * @param	string
 * @return 	string
 *
 */
if( ! function_exists("is_admin") ) 
{
	function is_admin($user_id = '')
	{
		$CI =& get_instance();
		$admin_group = $CI->config->item('admin_group');
		
		if(in_array($user_id,$admin_group)) 
		{
			return true;
		}	
		else
		{
			return false;
		}
	}
}

// ------------------------------------------------------------------------		

/**
 * Check user admin or client
 *
 * @access 	public
 * @param	string
 * @param	string
 * @param	string
 * @return 	string
 *
 */
if( ! function_exists("is_client") ) 
{
	function is_client($user_id = '')
	{
		$CI =& get_instance();
		$client_group = $CI->config->item('client_group');
		if(in_array($user_id,$client_group)) 
		{
			return true;
		}	
		else
		{
			return false;
		}
	}
}

// ------------------------------------------------------------------------		

/**
 * Set - Session Expiered Message
 *
 * @access 	public
 * @return 	string
 *
 */
if( ! function_exists("session_exp_msg") ) 
{
	function session_exp_msg()
	{
		echo '<div style="padding:10px;margin:2px;font-family:Arial, Helvetica, sans-serif;font-style:normal;width:100%;">
				<div style="color:#314A9D;font-size:16px;font-weight:bold;margin:0 0 5px; 0">Timeout</div>
				<div style="font-size:11px;vertical-align:middle;">Your browser has been left idle for some time.<br />
				As a security feature, we have logged you out of the system.<br /> 
				<a href="'.site_url("login").'" title="Click here to go to login page">Click here to go to login page</a>
				</div>
				</div>';
		exit();			
				
	}
}
?>